//
//  Joystick.m
//  Maze1
//
//  Created by Lin Tian on 10/14/11.
//  Copyright 2011 tina. All rights reserved.
//

#import "Joystick.h"


@implementation Joystick

@synthesize currentJs;
@synthesize lastJs;
@synthesize tap;
@synthesize player = _player;

+(CCScene*)scene
{
    CCScene* scene = [CCScene node];
    
    Joystick* aHud = [Joystick node];
    
    [scene addChild:aHud];
    
    return scene;
}

-(id)init//WithPlayer:(Player*)aPlayer
{
    if ((self = [super init])) {
        
        CCSprite* direction = [CCSprite spriteWithFile:@"joystick.png"];
        direction.position = CGPointMake(200, 200);
        direction.opacity = 200;
        direction.tag = DIRECTION;
        [self addChild:direction z:1 tag:DIRECTION];
        
        CCSprite* view = [CCSprite spriteWithFile:@"joystick.png"];
        view.position = CGPointMake(600, 200);
        view.opacity = 200;
        view.tag = VIEW;
        [self addChild:view z:1 tag:VIEW];
        
        //self.player = aPlayer;
        
        [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:1 swallowsTouches:YES];
    }
    return  self;
}

-(BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
    CGPoint touchLocation = [touch locationInView:[touch view]];
    touchLocation = [[CCDirector sharedDirector] convertToGL:touchLocation];
    touchLocation = [self convertToNodeSpace:touchLocation];
    
    //first figure out which joystick is selected
    currentJs = (CCSprite*)[self getChildByTag:DIRECTION];
    if (ccpDistance(touchLocation, currentJs.position)>=RADIUS){
        currentJs = (CCSprite*)[self getChildByTag:VIEW];
    }
    if(ccpDistance(touchLocation, currentJs.position)>=RADIUS){
        //this is a double tap for view joystick
        currentJs = nil;
    }
    
    //if user selected one joystick
    if(currentJs){
        if (isMoveJs){
          if(currentJs == lastJs) {
            //the user is moving one of the joystick
            currentJs.position = touchLocation;
          }else{
              //the user discard the double taps
              isMoveJs = NO;
          }
        }else{
            self.tap = [CCSprite spriteWithFile:@"touchPoint.png"];
            self.tap.position = touchLocation;
            [self addChild:self.tap];

            CGPoint direction = ccpSub(touchLocation, currentJs.position);
            if (ccpLength(direction) > 10) {
                //touch is not in the center of the circle
                //so deal with touch here.
                switch (currentJs.tag) {
                    case DIRECTION:
                    {
                        //the current joystick is for direction
                        float speed = self.player.deltaSpeed;
                        if (abs(direction.x) > abs(direction.y)) {
                            if (direction.x >0) {
                                self.player.speed = CGPointMake(speed, 0);
                            }else   {
                                self.player.speed = CGPointMake(-1*speed, 0);
                            }
                        }else {
                            if (direction.y >0) {
                                self.player.speed = CGPointMake(0, speed);
                            }else{
                                self.player.speed = CGPointMake(0, -1*speed);
                            }
                        }
                        break;
                    }
                    case VIEW:
                    {
                        //current selected joystick is for view
                        self.player.viewDirection = direction;
                        break;
                    }
                    default:
                        break;
                }
            }else{
                isTapped = YES;
            }
        }
    }
    return YES;
}

-(void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event
{
    if(currentJs){
        CGPoint touchLocation = [touch locationInView:[touch view]];
        touchLocation = [[CCDirector sharedDirector] convertToGL:touchLocation];
        touchLocation = [self convertToNodeSpace:touchLocation];
        
        if (isMoveJs) {
            //user is moving the current selected joystick
            currentJs.position = touchLocation;
        }else{
            CGPoint direction = ccpSub(touchLocation, currentJs.position);
            float dirLength = ccpLength(direction);
            if (dirLength > 10) {
                //user is using the selected joystick
                if (dirLength>RADIUS) {
                    direction = ccpMult(direction, RADIUS/dirLength);
                    touchLocation = ccpAdd(currentJs.position, direction);
                }
                self.tap.position = touchLocation;
                
                switch (currentJs.tag) {
                    case DIRECTION:
                    {
                        //the selected joystick is for direction
                        float speed = self.player.deltaSpeed;
                        if (abs(direction.x) > abs(direction.y)) {
                            if (direction.x >0) {
                                self.player.speed = CGPointMake(speed, 0);
                            }else   {
                                self.player.speed = CGPointMake(-1*speed, 0);
                            }
                        }else {
                            if (direction.y >0) {
                                self.player.speed = CGPointMake(0, speed);
                            }else{
                                self.player.speed = CGPointMake(0, -1*speed);
                            }
                        }
                        break;
                    }
                    case VIEW:
                    {
                        //the selected joystick is for view
                        /*if (direction.x > 0) {
                            [self.player.playerSprite getChildByTag:VIEWTAG].rotation= ccpAngle(direction, ccp(0,1))*(180.0/M_PI);
                        
                        }else{
                            self.player.playerSprite.rotation= 360.0 - ccpAngle(direction, ccp(0,1))*(180.0/M_PI);
                        }*/
                        self.player.viewDirection = direction;
                        break;
                    }
                    default:
                        break;
                }
                isTapped = NO;
            }
        }
    }
}

-(void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event
{
    if (isMoveJs) {
        isMoveJs = NO;
    }else{
        if (lastJs == currentJs && isTapped && touch.timestamp-lastTapTime < 300) {
            //this is a double tap, so next time user tap on this joystick, he moves the joystick
            isMoveJs = YES;
        }else{
            if (isTapped) {
                //if the program goes here, user tab on the selected joystick once.
                lastJs = currentJs;
                lastTapTime = touch.timestamp;
                isTapped = NO;
            }else{
                self.player.speed = CGPointMake(0, 0);
                lastJs = nil;
            }
        }
        [self removeChild:self.tap cleanup:YES];
    }
}


-(void)dealloc
{
    [currentJs release];
    currentJs = nil;
    
    [lastJs release];
    lastJs = nil;
    
    [tap release];
    tap = nil;
    
    [self.player release];
    self.player = nil;
    
    [[CCTouchDispatcher sharedDispatcher] removeDelegate:self];
    [super dealloc];
}
@end
